package mapEditor.view
{
	import asset.mapEditor.assetPanelAsset;
	
	import fl.containers.ScrollPane;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.*;
	import flash.text.TextFormat;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	import mapEditor.common.MapAssetManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.model.EditorModel;
	
	public class AssetPanelView extends assetPanelAsset
	{
		private var _model:EditorModel;
		private var _assetClassView:AssetClassView;
		private var _classScrollPane:ScrollPane;
		private var _itemPreview:*;		
		private var _assetPreview:DisplayObject;
		private var _assetPreviewBox:Sprite;
		
		public function AssetPanelView(model:EditorModel)
		{
			_model=model;
			
			setEvent();
			setView();
		}
		
		private function setView():void
		{
			if(assetPos.parent) assetPos.parent.removeChild(assetPos);
			_classScrollPane=new ScrollPane();
			_classScrollPane.width=160;
			_classScrollPane.x=assetPos.x;
			_classScrollPane.y=assetPos.y;
			addChild(_classScrollPane);
			_assetClassView=new AssetClassView();
			_classScrollPane.source=_assetClassView;
			btnAlpha.buttonMode=true;
			dropAssetLib.addItem({label:"全部资源库", data:"all"});
//			dropAssetLib.addItem({label:"111", data:"H:\\7Road\\Game\\Client\\Game.MapEditor\\map\\mapAsset.xml"});
		}
		
		private function setEvent():void
		{
			addEventListener(Event.ADDED_TO_STAGE, __onAddStage);
			addEventListener(Event.REMOVED_FROM_STAGE, __onRemovedFromStage);
			addEventListener(MouseEvent.MOUSE_OVER, __onMouseOver);
			_model.addEventListener(MapEditorEvent.MAP_ASSET_ACTION_ITEM_CHANGE, __onAssetActionItemChange);
			dropClass.addEventListener(Event.CHANGE, __onDropClassChange);
			dropAssetLib.addEventListener(Event.CHANGE, __onDropAssetLibChange);
			btnAddAssetLib.addEventListener(MouseEvent.CLICK, __onAddAssetLibClick);
			btnAlpha.addEventListener(MouseEvent.CLICK, __onAlphaClick);
		}
		
		private function removeEvent():void
		{
			removeEventListener(Event.ADDED_TO_STAGE, __onAddStage);
			removeEventListener(Event.REMOVED_FROM_STAGE, __onRemovedFromStage);
			removeEventListener(MouseEvent.MOUSE_OVER, __onMouseOver);
			_model.removeEventListener(MapEditorEvent.MAP_ASSET_ACTION_ITEM_CHANGE, __onAssetActionItemChange);
			dropClass.removeEventListener(Event.CHANGE, __onDropClassChange);
			dropAssetLib.removeEventListener(Event.CHANGE, __onDropAssetLibChange);
			btnAddAssetLib.removeEventListener(MouseEvent.CLICK, __onAddAssetLibClick);
			btnAlpha.removeEventListener(MouseEvent.CLICK, __onAlphaClick);
		}
		
		private function __onAlphaClick(evt:MouseEvent):void
		{
			if(alpha<=0.5)
			{
				alpha=1;
			}
			else
			{
				alpha=0.5;
			}
			
		}
		
		private function __onTitleMouseDown(evt:MouseEvent):void
		{
			startDrag();
		}
		
		private function __onTitleMouseUp(evt:MouseEvent):void
		{
			stopDrag();
		}
		
		/**
		 *当加载一个资源库成功 
		 */		
		private function __onMapAssetLoadComplete(evt:MapEditorEvent):void
		{
			var mapAssetManager:MapAssetManager=evt.currentTarget as MapAssetManager;
			mapAssetManager.removeEventListener(MapEditorEvent.MAP_ASSET_LOAD_COMPLETE, __onMapAssetLoadComplete);
			setAssetLibPathLoadCompleteByPath(mapAssetManager.path)
			mapAssetManager.dispose();
			mapAssetManager=null;
			
			loadAssetClass(evt.date.classes);
			loadAllClassAssetItem(evt.date.classes);
			
			if(checkAssetLibAllLoad()) dispatchEvent(new MapEditorEvent(MapEditorEvent.UPDATE_BY_ASSET_LIB_PATH_ASSET_LOAD_COMPLETE));
		}
		
		/**
		 *根据资源库路径，设置其已加载完成 
		 */		
		private function setAssetLibPathLoadCompleteByPath(path:String):void
		{
			for(var i:int=0;i<_model.mapVO.assetLibPath.length;i++)
			{
				if(_model.mapVO.assetLibPath[i].path==path)
				{
					_model.mapVO.assetLibPath[i].isLoad=true;
				}
			}
		}
		
		/**
		 *根据指定的库路径更新库数据 
		 */		
		public function updateByAssetLibPath(arrPath:Array):void
		{
			dropAssetLib.removeAll();//清除库列表
			dropAssetLib.addItem({label:"全部", data:"all"});
			
			dropClass.removeAll();//清除分类项
			_assetClassView.clearAllItem();//清除已加载的列表
			
			for(var i:int=0;i<arrPath.length;i++)
			{
				var arrFileName:Array=arrPath[i].path.split('/');
				dropAssetLib.addItem({label:arrFileName[arrFileName.length-1], data:arrPath[i].path});
				
				//加载资源库
				var mapAssetManager:MapAssetManager=new MapAssetManager();
				mapAssetManager.addEventListener(MapEditorEvent.MAP_ASSET_LOAD_COMPLETE, __onMapAssetLoadComplete);
				mapAssetManager.loadConfig(arrPath[i].path);
			}
		}
		
		/**
		 *载入所有已加入的的库资源 
		 */		
		private function loadAllAssetLib():void
		{
			if(dropAssetLib.length<=1) return;
			
			for(var i:int=0;i<dropAssetLib.length;i++)
			{
				var itemLib:Object = dropAssetLib.getItemAt(i);
				if(itemLib.data=="all") continue;
				
				//加载资源库
				var mapAssetManager:MapAssetManager=new MapAssetManager();
				mapAssetManager.addEventListener(MapEditorEvent.MAP_ASSET_LOAD_COMPLETE, __onMapAssetLoadComplete);
				mapAssetManager.loadConfig(itemLib.data);
			}
		}
		
		/**
		 *增加资源库配置文件 
		 */		
		private function __onAddAssetLibClick(evt:MouseEvent):void
		{
			var file:File = File.applicationDirectory;
			file.browseForOpen("请选择资源库文件");
			file.addEventListener(Event.SELECT,__onLibFileSelectHandle);
		}
		
		/**
		 *当选定完成资源库路径 
		 */		
		private function __onLibFileSelectHandle(e:Event=null):void
		{
			var arrFileName:Array=e.target.nativePath.toString().split('\\');
			var fileName:String=arrFileName[arrFileName.length-1];
			
			dropAssetLib.addItem({label:fileName, data:e.target.nativePath});
			
			if(dropAssetLib.selectedItem.data == "all")
			{
				if(!checkAssetLibPathExist(e.target.nativePath))
				{
					var assetLib:Object=new Object();
					assetLib.isLoad=false;
					assetLib.path=e.target.nativePath;
					_model.mapVO.assetLibPath.push(assetLib);//将库路径做记录，当保存地图时，此路径会被记录
				}
				
				var mapAssetManager:MapAssetManager=new MapAssetManager();
				mapAssetManager.addEventListener(MapEditorEvent.MAP_ASSET_LOAD_COMPLETE, __onMapAssetLoadComplete);
				mapAssetManager.loadConfig(e.target.nativePath);
			}
		}
		
		/**
		 *检查库路径是否已经存在 
		 */		
		private function checkAssetLibPathExist(path:String):Boolean
		{
			for(var i:int=0;i<_model.mapVO.assetLibPath.length;i++)
			{
				if(_model.mapVO.assetLibPath[i].path==path) return true;
			}
			return false;
		}
		
		/**
		 *检查库路径是否已经全部加载完成 
		 */		
		private function checkAssetLibAllLoad():Boolean
		{
			for(var i:int=0;i<_model.mapVO.assetLibPath.length;i++)
			{
				if(_model.mapVO.assetLibPath[i].isLoad==false) return false;
			}
			return true;
		}
		
		private function __onMouseOver(evt:MouseEvent):void
		{
			Mouse.show();
			Mouse.cursor=MouseCursor.AUTO;
		}
		
		private function __onAssetActionItemChange(evt:MapEditorEvent):void
		{
			if(_assetPreviewBox)
			{
				if(_assetPreviewBox.parent) _assetPreviewBox.parent.removeChild(_assetPreviewBox);
				_assetPreviewBox.removeEventListener(MouseEvent.MOUSE_OVER, __onAssetPreviewMouseOver);
				_assetPreviewBox.removeEventListener(MouseEvent.MOUSE_OUT, __onAssetPreviewMouseOut);
				_assetPreviewBox.removeEventListener(MouseEvent.MOUSE_DOWN, __onAssetPreviewMouseDown);
				_assetPreviewBox.removeEventListener(MouseEvent.MOUSE_UP, __onAssetPreviewMouseUp);
			}
			
			if(_assetPreview && _assetPreview.parent) _assetPreview.parent.removeChild(_assetPreview);
			_itemPreview=null;
			_assetPreview=null;
			_assetPreviewBox=null;
			
			_itemPreview=MapAssetManager.getAssetByType(_model.assetLink);
			if(!_itemPreview) return;
			
			switch(_model.assetClassType)
			{
				case "flash.display::BitmapData":
					_assetPreview=new Bitmap(_itemPreview as BitmapData);
					break;
				case "flash.display::MovieClip":
					_assetPreview=_itemPreview as MovieClip;
					break;
			}
			_itemPreview=null;
			
			if(_assetPreview)
			{
				_assetPreviewBox=new Sprite();
				_assetPreviewBox.addEventListener(MouseEvent.MOUSE_OVER, __onAssetPreviewMouseOver);
				_assetPreviewBox.addEventListener(MouseEvent.MOUSE_OUT, __onAssetPreviewMouseOut);
				_assetPreviewBox.addEventListener(MouseEvent.MOUSE_DOWN, __onAssetPreviewMouseDown);
				_assetPreviewBox.addEventListener(MouseEvent.MOUSE_UP, __onAssetPreviewMouseUp);
				_assetPreview.x=(previewBox.preview.width-_assetPreview.width)/2;
				_assetPreview.y=(previewBox.preview.height-_assetPreview.height)/2;
				_assetPreviewBox.addChild(_assetPreview);
				previewBox.preview.addChild(_assetPreviewBox);
			}
			
			resetActionItem();
		}
		
		/**
		 *复位物件选中状态 
		 */		
		private function resetActionItem():void
		{
			for each(var aclv:AssetClassListView in _assetClassView.itemList)
			{
				for each(var aciv:AssetClassItemView in aclv.itemList)
				{
					aciv.gotoAndStop(1);
				}
			}
		}
		
		/**
		 * 加载资源分类
		 */
		private function loadAssetClass(xmlList:XMLList, isClear:Boolean=false):void
		{
			if(isClear) dropClass.removeAll();
			if(!xmlList) return;
			
			for(var i:int=0;i<xmlList.length();i++)
			{
				dropClass.addItem({label:xmlList[i].@name, data:xmlList[i]});
			}
			
			var dropClassTextFormat:TextFormat=new TextFormat();
			dropClassTextFormat.size=12;
			dropClass.textField.setStyle("textFormat", dropClassTextFormat);
			dropClass.dropdown.setRendererStyle("textFormat", dropClassTextFormat);
		}
		
		/**
		 *加载所有分类物件列表 
		 */		
		private function loadAllClassAssetItem(xmlList:XMLList):void
		{
			if(!xmlList) return;
			
			for(var i:int=0;i<xmlList.length();i++)
			{
				loadAssetList(xmlList[i], false);
			}
		}
		
		/**
		 *当库中的分类发生选择时 
		 */		
		private function __onDropClassChange(evt:Event):void
		{
			var item:Object = dropClass.selectedItem;
			if(!item.data) return;
			
			loadAssetList(XML(item.data));
		}
		
		/**
		 *当资源库发生选择时 
		 */		
		private function __onDropAssetLibChange(evt:Event):void
		{
			dropClass.removeAll();//清除分类项
			_assetClassView.clearAllItem();//清除已加载的列表
			
			var item:Object = dropAssetLib.selectedItem;
			
			//如果选择了全部
			if(item.data=="all")
			{
				loadAllAssetLib();
			}
			else
			{
				var mapAssetManager:MapAssetManager=new MapAssetManager();
				mapAssetManager.addEventListener(MapEditorEvent.MAP_ASSET_LOAD_COMPLETE, __onMapAssetLoadComplete);
				mapAssetManager.loadConfig(item.data);
			}
		}
		
		/**
		 *加载类列表资源
		 */		
		private function loadAssetList(list:XML, isClearAll:Boolean=true):void
		{
			var xmlList:XMLList=list.classes;
			if(!xmlList) return;
			
			if(isClearAll) _assetClassView.clearAllItem();
			for(var i:int=0;i<xmlList.length();i++)
			{
				var item:AssetClassListView=new AssetClassListView(_model, xmlList[i]);
				_assetClassView.addEventListener(MapEditorEvent.MAP_ASSET_DOUBLE_CLICK, __onItemDoubleClick);
				_assetClassView.addItem(item);
			}
			
			_classScrollPane.refreshPane();
			_classScrollPane.update();
		}
		
		private function __onAddStage(evt:Event):void
		{
			stage.addEventListener(Event.RESIZE, __onReSize);
			panelBg.height=stage.stageHeight;
			setClassScrollPane();
			x=stage.stageWidth-37-171;
		}
		
		private function __onReSize(evt:Event):void
		{
			panelBg.height=stage.stageHeight;
			setClassScrollPane();
			x=stage.stageWidth-37-171;
		}
		
		private function setClassScrollPane():void
		{
			if(previewBox.visible)
			{
				_classScrollPane.height=stage.stageHeight-85-assetPos.y-5;	
			}
			else
			{
				_classScrollPane.height=stage.stageHeight-85-assetPos.y-5+125;
			}
		}
		
		private function __onRemovedFromStage(evt:Event):void
		{
			stage.removeEventListener(Event.RESIZE, __onReSize);
		}		
		
		private function __onAssetPreviewMouseOver(evt:MouseEvent):void
		{
			evt.stopImmediatePropagation();
			Mouse.show();
			Mouse.cursor=MouseCursor.HAND;
		}
		
		private function __onAssetPreviewMouseOut(evt:MouseEvent):void
		{
			evt.stopImmediatePropagation();
			Mouse.show();
			Mouse.cursor=MouseCursor.AUTO;
		}
		
		private function __onAssetPreviewMouseDown(evt:MouseEvent):void
		{
			evt.stopImmediatePropagation();
			_assetPreviewBox.startDrag();
		}
		
		private function __onAssetPreviewMouseUp(evt:MouseEvent):void
		{
			evt.stopImmediatePropagation();
			_assetPreviewBox.stopDrag();
		}
		
		private function __onItemDoubleClick(evt:MapEditorEvent):void
		{
			previewBox.visible=!previewBox.visible;
			if(previewBox.visible)
			{
				dropClass.y=185;
				_classScrollPane.y=assetPos.y;
			}
			else
			{
				dropClass.y-=125;
				_classScrollPane.y-=125;
			}
			setClassScrollPane();
		}
		
		public function dispose():void
		{
			removeEvent();
			_model=null;
			
			if(_assetClassView)
			{
				_assetClassView.removeEventListener(MapEditorEvent.MAP_ASSET_DOUBLE_CLICK, __onItemDoubleClick);
				if(_assetClassView.parent) _assetClassView.parent.removeChild(_assetClassView);
				_assetClassView.dispose();
			}
			_assetClassView=null;
			
			if(_classScrollPane && _classScrollPane.parent) _classScrollPane.parent.removeChild(_classScrollPane);
			_classScrollPane=null;
			
			if(_itemPreview)
			{
				if(_itemPreview && _itemPreview.parent) _itemPreview.parent.removeChild(_itemPreview);
				if(_itemPreview is BitmapData) (_itemPreview as BitmapData).dispose();
			}
			_itemPreview=null;
			
			if(_assetPreview && _assetPreview.parent) _assetPreview.parent.removeChild(_assetPreview);
			_assetPreview=null;
			
			if(_assetPreviewBox && _assetPreviewBox.parent) _assetPreviewBox.parent.removeChild(_assetPreviewBox);
			_assetPreviewBox=null;
			
			if(parent) parent.removeChild(this);
		}
	}
}